package com.kzw.oa.service.ext;

import java.util.List;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kzw.core.service.hibernate.DefaultEntityManager;
import com.kzw.core.util.ContextUtil;
import com.kzw.oa.model.system.Department;


@Service
@Transactional
public class DepartmentService2 extends DefaultEntityManager<Department, Long> {

	@Transactional(readOnly = true)
	public List<Department> getByParent(Long parentId) {
		String hql = "from Department where parentId=? order by sn asc, createtime asc";
		List<Department> result = findByHQL(hql, parentId);
		return result;
	}

	@Cacheable("orgAll")
	@Transactional(readOnly = true)
	public String treeAll() {
		// 获得树根，可能存在多个根
		String hql = "from Department where parentId=0";
		List<Department> orgs = findByHQL(hql);
		StringBuffer buff = new StringBuffer("[");
		for (Department orgRoot : orgs) {
			buff.append("{id:'" + orgRoot.getDepId() + "',text:'"
					+ orgRoot.getShortName() + "',orgType:'"+orgRoot.getOrgType()
					+"',expanded:true,children:[");
			List<Department> orgList = getByParent(orgRoot.getDepId());
			for (Department org : orgList) {
				buff.append("{id:'" + org.getDepId())
						.append("',text:'" + org.getShortName().trim())
						.append("',").append("orgType:'")
						.append(org.getOrgType()).append("',");
				buff.append(getChildList(org.getDepId()));
			}
			if (orgList.size() > 0) {
				buff.deleteCharAt(buff.length() - 1);
			}
			buff.append("]},");
		}
		if (orgs.size() > 0) {
			buff.deleteCharAt(buff.length() - 1);
		}
		buff.append("]");

		return buff.toString();
	}
	
	@Transactional(readOnly = true)
	public String tree() {
		// 显示本单位下的用户		
		String hql = "from Department where depId=" + ContextUtil.getCurrentDept().getDepId();
		List<Department> orgs = findByHQL(hql);
		StringBuffer buff = new StringBuffer("[");
		for (Department orgRoot : orgs) {
			buff.append("{id:'" + orgRoot.getDepId() + "',text:'"
					+ orgRoot.getShortName() + "',orgType:'"+orgRoot.getOrgType()
					+"',expanded:true,children:[");
			List<Department> orgList = getByParent(orgRoot.getDepId());
			for (Department org : orgList) {
				buff.append("{id:'" + org.getDepId())
						.append("',text:'" + org.getShortName().trim())
						.append("',").append("orgType:'")
						.append(org.getOrgType()).append("',");
				buff.append(getChildList(org.getDepId()));
			}
			if (orgList.size() > 0) {
				buff.deleteCharAt(buff.length() - 1);
			}
			buff.append("]},");
		}
		if (orgs.size() > 0) {
			buff.deleteCharAt(buff.length() - 1);
		}
		buff.append("]");

		return buff.toString();
	}

	private String getChildList(Long parentId) {
		StringBuffer buff = new StringBuffer();
		List<Department> orgList = getByParent(parentId);
		if (orgList.size() == 0) {
			buff.append("leaf:true,expanded:true},");
			return buff.toString();
		} else {
			buff.append("expanded:false,children:[");
			for (Department org : orgList) {
				buff.append("{id:'" + org.getDepId())
						.append("',text:'" + org.getShortName().trim())
						.append("',").append("orgType:'")
						.append(org.getOrgType()).append("',");
				buff.append(getChildList(org.getDepId()));
			}
			buff.deleteCharAt(buff.length() - 1);
			buff.append("]},");
			return buff.toString();
		}
	}

	/**
	 * 显示到指定depth深度的单位
	 * @param depth 
	 * */
	public String treeByDepth(int depth) {
		// 获得树根，可能存在多个根
		String hql = "from Department where parentId=0";
		List<Department> orgs = findByHQL(hql);
		StringBuffer buff = new StringBuffer("[");
		for (Department orgRoot : orgs) {
			buff.append("{id:'" + orgRoot.getDepId() + "',text:'"
					+ orgRoot.getShortName() + "',orgType:'"+orgRoot.getOrgType()
					+"',expanded:true,children:[");
			List<Department> orgList = getByParent(orgRoot.getDepId());
			for (Department org : orgList) {
				buff.append("{id:'" + org.getDepId())
						.append("',text:'" + org.getShortName().trim())
						.append("',").append("orgType:'")
						.append(org.getOrgType()).append("',");
				if(org.getDepth() <= depth) {
					buff.append(getChildList(org.getDepId()));
				}
			}
			if (orgList.size() > 0) {
				buff.deleteCharAt(buff.length() - 1);
			}
			buff.append("]},");
		}
		if (orgs.size() > 0) {
			buff.deleteCharAt(buff.length() - 1);
		}
		buff.append("]");

		return buff.toString();
	}


}
